﻿using System;
using System.Windows;
using System.Windows.Interactivity;
using GameLogic.Controls;

namespace GameLogic.Logic
{
    // Template for GameTriggerActions triggered by GameTriggers
    public abstract class GameTriggerAction<T> : TriggerAction<T> where T : FrameworkElement
    {
        private GameContainerFinder gameContainerFinder;

        public GameContainer GameContainer
        {
            get { return this.gameContainerFinder.GameContainer; }
        }

        protected override void OnAttached()
        {
            base.OnAttached();
            this.gameContainerFinder = new GameContainerFinder(this.AssociatedObject);
            this.gameContainerFinder.OnGameContainerLoaded += this.OnGameContainerLoaded;
        }

        protected virtual void OnGameContainerInitialized()
        {
        }

        protected override void OnDetaching()
        {
            base.OnDetaching();
            this.gameContainerFinder.OnGameContainerLoaded -= this.OnGameContainerLoaded;
            this.gameContainerFinder = null;
        }

        private void OnGameContainerLoaded(object sender, EventArgs e)
        {
            this.OnGameContainerInitialized();
        }
    }
}
